﻿#region Using Directives

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using QuickDev.Common.Audit;

using QuickDev.Localization;
using QuickDev.Model;
using QuickDev.Common.Utils;

#endregion

namespace QuickDev.Web.App.Administration.Controls.SiteSettingConfiguration
{
    public partial class general : SiteSettingsControl
    {
        #region Member Variables


        #endregion

        #region Page Events

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected override void Page_Load( object sender, EventArgs e )
        {
            try
            {
                base.Page_Load( sender, e );
                if ( view.Equals( "g" ) )
                {
                    SetGeneralSettingsProperties();
                    if ( !Page.IsPostBack )
                    {
                        SetThemeSelections();
                        SetMaximumReturnOrderItemsDropDown();
                        txtSiteLogo.Text = SiteSettings.SiteLogo;
                        ddlTheme.SelectedValue = SiteSettings.Theme;
                        ddlMaxAges.SelectedValue = SiteSettings.MaximumReturnOrderItems.ToString();
                        txtSiteName.Text = SiteSettings.SiteName;
                        txtTagLine.Text = SiteSettings.TagLine;
                        chkRequireSsl.Checked = SiteSettings.RequireSsl;
                        txtNewsFeedUrl.Text = SiteSettings.NewsFeedUrl;
                        txtDomain.Text = SiteSettings.Domain;
                        txtUrl.Text = SiteSettings.Url;
                    }
                }
            }
            catch ( Exception ex )
            {
                Logger.Error( typeof( general ).Name + ".Page_Load", ex );
                base.MasterPage.MessageCenter.DisplayCriticalMessage( ex.Message );
            }
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void btnSave_Click( object sender, EventArgs e )
        {
            try
            {
                SiteSettings.SiteLogo = txtSiteLogo.Text;
                SiteSettings.Theme = ddlTheme.SelectedValue;
                SiteSettings.MaximumReturnOrderItems = (int)Utility.ChangeTypeTo( ddlMaxAges.SelectedValue, typeof( Int32 ) );
                SiteSettings.SiteName = txtSiteName.Text.Trim();
                SiteSettings.TagLine = txtTagLine.Text.Trim();
                SiteSettings.RequireSsl = chkRequireSsl.Checked;
                SiteSettings.NewsFeedUrl = txtNewsFeedUrl.Text;
                SiteSettings.Domain = txtDomain.Text;
                SiteSettings.Url = txtUrl.Text;

                ServiceConfiguration model = base.SaveConfiguration( SiteSettings );
                if ( model.ConfigurationId > 0 )
                {
                    MasterPage.MessageCenter.DisplaySuccessMessage( LocalizationUtility.GetText( "lblSiteSettingsSaved" ) );
                }
                else
                {
                    MasterPage.MessageCenter.DisplayFailureMessage( LocalizationUtility.GetText( "lblSiteSettingsNotSaved" ) );
                }
            }
            catch ( Exception ex )
            {
                Logger.Error( typeof( general ).Name + ".btnSave_Click", ex );
                base.MasterPage.MessageCenter.DisplayCriticalMessage( ex.Message );
            }
        }

        #endregion

        #region Methods

        #region Private

        /// <summary>
        /// Sets the theme selections.
        /// </summary>
        private void SetThemeSelections()
        {
            string[] themes = Directory.GetDirectories( Server.MapPath( "~/App_Themes" ) );
            string path = string.Empty;
            for ( int i = 0; i < themes.Length; i++ )
            {
                themes[i] = new DirectoryInfo( themes[i] ).Name;
            }
            ddlTheme.DataSource = themes;
            ddlTheme.DataBind();
        }

        /// <summary>
        /// SetMaximumProductsToAddToCartDropDown
        /// </summary>
        private void SetMaximumReturnOrderItemsDropDown()
        {
            for ( int i = 1; i <= 100; i++ )
            {
                ddlMaxAges.Items.Add( new ListItem( i.ToString(), i.ToString() ) );
            }
        }

        /// <summary>
        /// Sets the general settings properties.
        /// </summary>
        private void SetGeneralSettingsProperties()
        {
            this.Page.Title = string.Format( WebUtility.MainTitleTemplate, base.MasterPage.SiteSettings.SiteName, LocalizationUtility.GetText( "titleSiteSettingsGeneralLayout" ) );

            hlImageSelector.NavigateUrl = string.Format( "~/Administration/ImageSelector.aspx?view=s&controlId={0}", txtSiteLogo.ClientID );
        }

        #endregion

        #endregion

    }
}